package com.clever.mall.controller;

import com.clever.mall.commons.dto.ProductRequestParam;
import com.clever.mall.commons.exception.BaseException;
import com.clever.mall.commons.result.BaseResult;
import com.clever.mall.service.ProductService;
import com.clever.mall.vo.ProductVo;
import com.clever.mall.vo.SearchResult;
import com.fasterxml.jackson.databind.ser.Serializers;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Resource
    ProductService productService;

    //查找热门商品
    @RequestMapping("/findHeartProduct")
    public BaseResult findHeartProduct(){
        List<ProductVo> heatProduct = productService.findHeatProduct();
        return BaseResult.success(heatProduct);
    }

    //以id查找商品详情
    @RequestMapping("/findById")
    public BaseResult findById(@NotNull int id){
        ProductVo byId = productService.findById(id);
        return BaseResult.success(byId);
    }

    //以分类id查找所有商品
    @RequestMapping("/findAllByCateGoryId")
    public BaseResult findAllByCateGoryId(@NotNull int id){
        List<ProductVo> allByCateGoryId = productService.findAllByCateGoryId(id);
        return BaseResult.success(allByCateGoryId);
    }

    //以关键字查询商品和用户
    @RequestMapping("/findProductsAndUserByKeyword")
    public BaseResult findProductsAndUserByKeyword(String keyword) {
        SearchResult re = productService.findProductsAndUserByKeyword(keyword);
        return BaseResult.success(re);
    }

    //多条件过滤筛选商品
    @PostMapping("/findProductByConditions")
    public BaseResult findProductByConditions(@RequestBody ProductRequestParam requestParam){
        List<ProductVo> byConditions = productService.findByConditions(requestParam);
        return BaseResult.success(byConditions);
    }

    //活动id查询参与此活动的商品
    @RequestMapping("/findProductByActId")
    public BaseResult findProductByActId(@NotNull int id){
        List<ProductVo> byActId = productService.findByActId(id);
        return BaseResult.success(byActId);
    }
}
